package dao

import (
	"gitee.com/cyanbleeze/go-image-management/internal/model"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
)

func (d *Dao) CreateLike(uID uint, pID uint) error {
	like := model.Like{
		UserID:    uID,
		PictureID: pID,
		State:     true,
	}
	return like.Create(d.engine)
}

func (d *Dao) UpdateLike(id uint, state bool) error {
	like := model.Like{
		State: state,
	}
	like.ID = id
	return like.Update(d.engine)
}

func (d *Dao) DeleteLike(id uint, uID uint) error {
	like := model.Like{
		UserID: uID,
	}
	like.ID = id
	return like.Delete(d.engine)
}

func (d *Dao) GetLike(uID uint, pID uint) (*model.Like, error) {
	like := model.Like{
		UserID:    uID,
		PictureID: pID,
	}
	return like.Get(d.engine)
}
func (d *Dao) GetLikeOrCreate(uID uint, pID uint) (*model.Like, error) {
	like := model.Like{
		UserID:    uID,
		PictureID: pID,
	}
	return like.GetOrCreate(d.engine)
}
func (d *Dao) GetLikeListByPictureID(pID uint, page int, pageSize int) ([]*model.Like, error) {
	like := model.Like{
		PictureID: pID,
	}
	return like.List(d.engine, app.GetPageOffset(page, pageSize), pageSize)
}

func (d *Dao) CountLikesByPictureID(pID uint, state bool) (int, error) {
	like := model.Like{
		PictureID: pID,
		State:     state,
	}
	return like.Count(d.engine)
}
